<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

/**
 * 消息控制器
 * @package app\user\admin
 */
class Taskmsg extends Admin
{
    /**
     * 消息列表
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $map = $this->getMap();
        $map[] = ['type','=',2];
        $data_list = db::name('message_log')->where($map)
            ->order($this->getOrder('id DESC'))
            ->paginate();

        return ZBuilder::make('table')
            ->setTableName('message_log')
            ->addTopButton('add')
            ->addTopButton('delete')
            ->addRightButton('delete')
            ->addColumns([
                ['__INDEX__', 'ID'],
                ['title', '消息'],
                ['addtime', '时间', 'datetime'],
                ['right_button', '操作', 'btn'],
            ])
            ->setRowList($data_list)
            ->fetch();
    }

    /**
     * 新增
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     * @throws \think\Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if(empty($data['title'])){
                $this->error('请添加消息内容');
            }
            $data['type'] = 2;
            $data['addtime'] = time();
            $res = db::name('message_log')->insert($data);
            
            if ($res) {
                $this->success('新增成功', 'index');
            } else {
                $this->error('新增失败');
            }
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'title', '消息'],
            ])
            ->fetch();
    }
}
